/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the seckill4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.jalonjia.seckill.core.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jalonjia.seckill.common.core.constant.CacheConstants;
import com.jalonjia.seckill.common.core.util.R;
import com.jalonjia.seckill.common.log.annotation.SysLog;
import com.jalonjia.seckill.common.security.util.SecurityUtils;
import com.jalonjia.seckill.core.entity.SeckillActivity;
import com.jalonjia.seckill.core.entity.SeckillTarget;
import com.jalonjia.seckill.core.service.SeckillActivityService;
import com.jalonjia.seckill.core.service.SeckillTargetService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 秒杀对象表
 *
 * @author 贾晓超
 * @date 2022-05-05 20:56:44
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/seckilltarget")
@Tag(name = "秒杀对象表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SeckillTargetController {
	private final SeckillTargetService seckillTargetService;
	private final SeckillActivityService seckillActivityService;

	/**
	 * 分页查询
	 *
	 * @param page          分页对象
	 * @param seckillTarget 秒杀对象表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_get')")
	public R getSeckillTargetPage(Page page, SeckillTarget seckillTarget) {
		if (!SecurityUtils.isAdmin()) {
			seckillTarget.setCreateBy(SecurityUtils.username());
		}
		return R.ok(seckillTargetService.page(page, Wrappers.query(seckillTarget).orderByDesc("create_time")));
	}

	/**
	 * 秒杀对象列表查询
	 *
	 * @return
	 */
	@Operation(summary = "秒杀对象列表查询", description = "秒杀对象列表查询")
	@GetMapping("/dict")
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_get')")
	public R getSeckillTargetDict() {
		return R.ok(seckillTargetService.getTargetDict());
	}

	/**
	 * 通过id查询秒杀对象表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(seckillTargetService.getById(id));
	}

	/**
	 * 新增秒杀对象表
	 *
	 * @param seckillTarget 秒杀对象表
	 * @return R
	 */
	@Operation(summary = "新增秒杀对象表", description = "新增秒杀对象表")
	@SysLog("新增秒杀对象表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_add')")
	public R save(@RequestBody SeckillTarget seckillTarget) {
		return R.ok(seckillTargetService.save(seckillTarget));
	}

	/**
	 * 修改秒杀对象表
	 *
	 * @param seckillTarget 秒杀对象表
	 * @return R
	 */
	@Operation(summary = "修改秒杀对象表", description = "修改秒杀对象表")
	@SysLog("修改秒杀对象表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_edit')")
	@CacheEvict(value = CacheConstants.SECKILL_DETAIL, allEntries = true)
	public R updateById(@RequestBody SeckillTarget seckillTarget) {
		return R.ok(seckillTargetService.updateById(seckillTarget));
	}

	/**
	 * 通过id删除秒杀对象表
	 *
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除秒杀对象表", description = "通过id删除秒杀对象表")
	@SysLog("通过id删除秒杀对象表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('core_seckilltarget_del')")
	@CacheEvict(value = CacheConstants.SECKILL_DETAIL, allEntries = true)
	public R removeById(@PathVariable Long id) {
		List<SeckillActivity> activityList = seckillActivityService.query().eq("target_no", id).list()
				.stream().filter(activity -> seckillActivityService.duringActivity(activity)).collect(Collectors.toList());
		if (activityList.size() > 0) {
			return R.failed("存在正在进行的秒杀活动:" + activityList.stream().map(SeckillActivity::getTitle).collect(Collectors.toList()));
		}
		return R.ok(seckillTargetService.removeById(id));
	}

}
